package camuyen.ltwlibrary.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import camuyen.ltwlibrary.model.Book;
import camuyen.ltwlibrary.util.HibernateUtil;

public class BookDao {
	public static Book getBook(Long bookId) {
        Session session = HibernateUtil.openSession();
        Book book = (Book) session.load(Book.class, bookId);
        session.close();
        return book;
    }

    public static void saveBook(Book book) {
        Session session = HibernateUtil.openSession();
        session.getTransaction().begin();
        session.save(book);
        session.getTransaction().commit();
        session.close();
    }

    public static void updateBook(Book book) {
        Session session = HibernateUtil.openSession();
        session.getTransaction().begin();
        session.update(book);
        session.getTransaction().commit();
        session.close();
    }
    
    public static List<Book> getAll() {
    	Session session = HibernateUtil.openSession();
    	Query query = session.createQuery("SELECT b FROM Book b ORDER BY b.bookId DESC");
    	List<Book> allBook = (List<Book>) query.list();
        session.close();
    	return allBook;
    }
}
